package com.zhongyouyun.wx.wxapplet.cqyc.controller;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhongyouyun.config.RedisConfig;
import com.zhongyouyun.cqyc.model.ComplaintReminderModel;
import com.zhongyouyun.cqyc.model.ComplaintTitleAnswerModel;
import com.zhongyouyun.cqyc.model.ComplaintTitleAnswerReportModel;
import com.zhongyouyun.cqyc.service.ComplaintReminderService;
import com.zhongyouyun.cqyc.service.ComplaintTitleAnswerReportService;
import com.zhongyouyun.cqyc.service.ComplaintTitleAnswerService;
import com.zhongyouyun.other.model.Result;
import com.zhongyouyun.wx.wxapplet.cqyc.pojo.ComplaintAnswerPojo;
import com.zhongyouyun.wx.wxapplet.cqyc.pojo.ComplaintPojo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("wx/wxapplet/cqyc/complaint")
@Api(value = "烟草城区-微信小程序投诉操作", tags = {"烟草城区-微信小程序投诉操作"})
public class WxAppletComplaintController {
    @Autowired
    private ComplaintReminderService complaintReminderService;
    @Autowired
    private ComplaintTitleAnswerService complaintTitleAnswerService;
    @Autowired
    private ComplaintTitleAnswerReportService complaintTitleAnswerReportService;


    @GetMapping("/findComplaint")
    @ResponseBody
    @ApiOperation(value = "微信小程序投诉显示页面", notes = "微信小程序投诉显示页面")
    public Result<ComplaintAnswerPojo> findComplaint() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("state","1");
        ComplaintAnswerPojo complaintAnswerPojo = new ComplaintAnswerPojo();
        ComplaintReminderModel complaintReminderModel = complaintReminderService.getById(0L);
        if (ObjectUtil.isNotEmpty(complaintReminderModel)) {

            List<ComplaintTitleAnswerModel> complaintTitleAnswerModelList = complaintTitleAnswerService.selectList(queryWrapper);
            complaintAnswerPojo.setList(complaintTitleAnswerModelList);
            return Result.<ComplaintAnswerPojo>builder().message("成功").status(0).data(complaintAnswerPojo).build();
        } else {
            List<ComplaintTitleAnswerModel> complaintTitleAnswerModelList = complaintTitleAnswerService.selectList(queryWrapper);
            complaintAnswerPojo.setList(complaintTitleAnswerModelList);
            return Result.<ComplaintAnswerPojo>builder().message("成功").status(0).data(complaintAnswerPojo).build();
        }
    }

    @PostMapping("/saveReport")
    @ResponseBody
    @ApiOperation(value = "微信小程序投诉保存", notes = "微信小程序投诉保存")
    public Result saveReport(@RequestBody ComplaintPojo complaintPojo) {
        if (ObjectUtil.isNotEmpty(complaintPojo)) {
            Date date = new Date();
            ComplaintTitleAnswerReportModel complaintTitleAnswerReportModel = new ComplaintTitleAnswerReportModel();
            complaintTitleAnswerReportModel.setWxId(complaintPojo.getWxUserId());
            complaintTitleAnswerReportModel.setContact(complaintPojo.getContact());
            complaintTitleAnswerReportModel.setAnswerContext(complaintPojo.getText());
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("wx_id",complaintPojo.getWxUserId());
            queryWrapper.orderByDesc("createTime");
            List<ComplaintTitleAnswerReportModel> list = complaintTitleAnswerReportService.selectList(queryWrapper);
            if(list.size() > 0){
                ComplaintTitleAnswerReportModel reportModel = list.get(0);
                long time = reportModel.getCreateTime().getTime();
                long time1 = date.getTime();
                if((time1 - time) > 60000){
                    complaintTitleAnswerReportModel.setCreateTime(date);
                    complaintTitleAnswerReportService.saveOrUpdate(complaintTitleAnswerReportModel);
                }else{
                    return Result.builder().message("举报限制，一分钟内只能举报一次").success(false).status(1).build();
                }
            }else{
                complaintTitleAnswerReportModel.setCreateTime(date);
                complaintTitleAnswerReportService.saveOrUpdate(complaintTitleAnswerReportModel);
            }
            return Result.builder().message("保存成功").status(0).build();
        } else {
            return Result.builder().message("保存失败").success(false).status(1).build();
        }
    }

}
